home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 11
/
Cream of the Crop 11-1.iso
/
windows
/
boyer04.zip
/
DEMO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-01-15
|
8KB
|
228 lines
/*-----------------------------------------------------------------------------
file: demo.c
desc: Boyer-Moore text search algorithm demo (Windows version)
by: Patrick Ko
date: 6 Mar 91 - born
revi: 4 Apr 94 - port Windows 3.1
note: use huge pointers to cater for big contiguous memory
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <windowsx.h>
#include <memory.h>
#include "boyer.h"
#include "demo.h"
long FAR PASCAL _export WndProc (HWND, UINT, UINT, LONG) ;
static char szAppName [] = "Demo" ;
static char temp[128];
static HPSTR ss = NULL;
static HPSTR sf = NULL;
static HPSTR sb = NULL;
static HPSTR sfic = NULL;
static HPSTR sbic = NULL;
static int msg = 0;
static HFIND hfind;
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, "Boyer-Moore Algorithm Demonstration",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
long FAR PASCAL _export WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
{
HMENU hMenu;
static short cxChar, cxCaps, cyChar;
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
long i;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return 0;
case WM_COMMAND:
hMenu = GetMenu (hwnd) ;
switch (wParam)
{
case IDM_GO:
msg = 1;
InvalidateRect(hwnd, NULL, FALSE);
SendMessage(hwnd, WM_PAINT, 0, 0L);
/* allocate around 1M space */
ss = (char huge *)GlobalAllocPtr(GMEM_MOVEABLE, SPACE_1M);
/* initialize the space with texts */
for (i=0; i<SPACE_1M; i+=4)
{
/*
* only apply in large+ model where memcpy is
* large by default
*/
memcpy( (HPSTR)ss + i, "wild", 4 );
}
/* put our targets "tiger" and "TIGER" */
memcpy( (HPSTR)ss + tiger_POSITION, "tiger", 5 );
memcpy( (HPSTR)ss + TIGER_POSITION, "TIGER", 5 );
msg++;
InvalidateRect(hwnd, NULL, FALSE);
SendMessage(hwnd, WM_PAINT, 0, 0L);
/* setup what you want to search */
hfind = SetFindPattern( "tiger" );
/* search string in forward manner */
sf = Find(hfind, (HPSTR)ss, SPACE_1M);
/* search string in backward manner */
/* be careful not to specify a "huge pointer" outside */
/* what you have allocated in Windows, since Windows */
/* huge library does some work to translate your ptr */
/* into the GlobalAlloc() space */
sb = FindBackward(hfind, (HPSTR)ss + SPACE_1M - 1, SPACE_1M);
/* search string in case insensitive forward manner */
sfic = FindIC(hfind, (HPSTR)ss, SPACE_1M);
/* search string in backward manner */
/* be careful not to specify a "huge pointer" outside */
/* what you have allocated in Windows, since Windows */
/* huge library does some work to translate your ptr */
/* into the GlobalAlloc() space */
sbic = FindBackwardIC(hfind, (HPSTR)ss + SPACE_1M - 1, SPACE_1M);
GlobalFreePtr(ss);
FreeFindPattern(hfind);
msg++;
InvalidateRect(hwnd, NULL, FALSE);
SendMessage(hwnd, WM_PAINT, 0, 0L);
return 0 ;
case IDM_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
return 0 ;
case IDM_ABOUT:
MessageBox (hwnd, "Boyer-Moore Algorithm Demo",
szAppName, MB_ICONINFORMATION | MB_OK) ;
return 0 ;
}
break ;
case WM_PAINT:
switch (msg)
{
case 1:
hdc = BeginPaint(hwnd, &ps);
wsprintf(temp, "initializing search space with \"wild tiger and TIGER\"");
TextOut(hdc, cxChar, cyChar * 1, temp, lstrlen(temp));
EndPaint(hwnd, &ps);
break;
case 2:
hdc = BeginPaint(hwnd, &ps);
wsprintf(temp, "begin search:");
TextOut(hdc, cxChar, cyChar * 2, temp, lstrlen(temp));
wsprintf(temp, "searching for word \"tiger\" in 1M space:");
TextOut(hdc, cxChar, cyChar * 3, temp, lstrlen(temp));
EndPaint(hwnd, &ps);
break;
case 3:
if (ss == NULL) break;
hdc = BeginPaint(hwnd, &ps);
if (sf == NULL)
wsprintf(temp, "Find() pattern not found");
else
wsprintf(temp, "Find() pattern found at offset = %lu",
(DWORD)((HPSTR)sf - (HPSTR)ss));
TextOut(hdc, cxChar, cyChar * 4, temp, lstrlen(temp));
if (sb == NULL)
wsprintf(temp, "FindBackward() pattern not found");
else
wsprintf(temp, "FindBackward() pattern found at offset = %lu",
(DWORD)((HPSTR)((HPSTR)ss + SPACE_1M - 1) - (HPSTR)sb));
TextOut(hdc, cxChar, cyChar * 5, temp, lstrlen(temp));
if (sfic == NULL)
wsprintf(temp, "FindIC() pattern not found");
else
wsprintf(temp, "FindIC() pattern found at offset = %lu",
(DWORD)((HPSTR)sfic - (HPSTR)ss));
TextOut(hdc, cxChar, cyChar * 6, temp, lstrlen(temp));
if (sbic == NULL)
wsprintf(temp, "FindBackwardIC() pattern not found");
else
wsprintf(temp, "FindBackwardIC() pattern found at offset = %lu",
(DWORD)((HPSTR)((HPSTR)ss + SPACE_1M - 1) - (HPSTR)sbic));
TextOut(hdc, cxChar, cyChar * 7, temp, lstrlen(temp));
EndPaint(hwnd, &ps);
break;
default:
break;
}
break;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}